<?php
if (!defined("IN_OP"))
{
    exit("You can't access this file directly...");
}
require_once( "class_3d/CheckMode_3d.php" );
$gametype = 1;
include_once( "class/function_3d.php" );
$ui = new UI_3D(true);
$tpl = new Template2();
$conf = new Config();
$pagecount = intval($conf->get("page_max_count"));
if ($curruser['enabled'] == 0)
{
    $ui->showErrorPage("帐号已被暂停使用");
    exit();
}
$tmp = "";
$sum_amount = 0;
$sum_result = 0;
$sum_back1 = 0;
$postTime = $conf->get("paper_post_bet");
if (date("Hi") <= $postTime)
{
    $todayInt = strtotime("-1 day");
    $today = date("Y-m-d", $todayInt);
}
else
{
    $todayInt = time();
    $today = date("Y-m-d", $todayInt);
}
$dateTempM = date("m", $todayInt);
$dateTempD = date("d", $todayInt);
$dateTempW = date("w", $todayInt);
$dateTempY = date("Y", $todayInt);
if ($dateTempW == 0)
{
    $secThisWeek1 = mktime(0, 0, 0, $dateTempM, $dateTempD - 6, $dateTempY);
}
else
{
    $secThisWeek1 = mktime(0, 0, 0, $dateTempM, $dateTempD - $dateTempW + 1, $dateTempY);
}
$calThisWeek1 = date("Y-m-d", $secThisWeek1);
$calLastWeek1 = date("Y-m-d", $secThisWeek1 - 604800);
$calLastWeek2 = date("Y-m-d", $secThisWeek1 - 1);
$minday = getPaperMinDate(0 - 1);
$begin = getvar("begin") == "" ? $calThisWeek1 : getvar("begin");
$end = getvar("end") == "" ? $today : getvar("end");
$check = getvar("begin") == "" ? "2" : getvar("check");
switch($check)
{
	case 1:
		$tpl->set('class1','btnchoiced');
		break;	
	case 2:
		$tpl->set('class2','btnchoiced');
		break;	
	case 3:
		$tpl->set('class3','btnchoiced');
		break;	
}
$tpl->set(array("today" => $today, "minday" => $minday, "calThisWeek1" => $calThisWeek1, "calLastWeek1" => $calLastWeek1, "calLastWeek2" => $calLastWeek2, "gametype" => $gametype, "d", "begin" => $begin, "end" => $end));
if ($today == $end)
{
    $oneday = 86400;
}
else
{
    $oneday = 0;
}
$today = max($end, $minday);
$minday = max($begin, $minday);
$lastday = date("Y-m-d", strtotime($today) - $oneday);
foreach ($g_gametype_3d as $key => $value)
{
    $game_3d3s[$key] = $key;
}
$game_3d3s[0] = "";
$sqlArr = array();
foreach ($g_gametype_3d as $key => $val)
{
    if (!$g_init_mode_6h && $key == 0)
    {
        continue;
    }
    if (!$g_init_mode_3d && $key === $game_3d3s[$key])
    {
        continue;
    }
    if ($g_menu_enabled[$key] == 0)
    {
        continue;
    }
    $tempBet = "bet";
    if ($val['en'] == "6h")
    {
        $tempTbl = "";
    }
    else
    {
        $tempTbl = "_" . $val['en'];
        $tempBet .= $tempTbl;
    }
    if ($conf->get("calcbet{$tempTbl}") == 1)
    {
        $ui->showErrorPage($val['ch'] . "颁奖过帐中，无法查询全部资料，请稍後再使用本功能！", true);
    }
    else if ($conf->get("systemcheck_" . $val['en']) == 1)
    {
        $ui->showErrorPage($val['ch'] . "系统维謢中，无法查询全部资料，请稍後再使用本功能！", true);
    }
    else if (0 < $key && $conf->get("paper_post_flag") == 1)
    {
        $ui->showErrorPage($val['ch'] . "系统维謢中，无法查询全部资料，请稍後再使用本功能！", true);
    }
    if ($key == 0)
    {
        $sql = " SELECT sum(a.amount) as amount, sum(a.a1) as a1, sum(a.b1) as b1, b.open, {$key} as gametype FROM {$tempBet} a";
        $sql .= " LEFT JOIN lotto{$tempTbl} b ON (b.id = a.lotto_id)";
        $sql .= " WHERE a.user_id =" . $curruser['id'] . " AND a.rame IN(0,9) AND b.open BETWEEN '{$minday}' AND '{$today}' GROUP BY b.open";
    }
    else
    {
        $sql = " SELECT sum(a.amount) as amount, sum(a.a1) as a1, sum(a.b1) as b1, b.open, {$key} as gametype FROM {$tempBet} a";
        $sql .= " LEFT JOIN lotto{$tempTbl} b ON (b.id = a.lotto_id)";
        $sql .= " WHERE a.user_id =" . $curruser['id'] . " AND a.rame IN(0,9) AND b.open ='{$today}' GROUP BY b.open";
        array_push($sqlArr, $sql);
        $sql = " SELECT sum(a.amount) as amount, sum(a.a1) as a1, sum(a.b1) as b1, b.open, {$key} as gametype FROM x{$tempBet} a";
        $sql .= " LEFT JOIN lotto{$tempTbl} b ON (b.id = a.lotto_id)";
        $sql .= " WHERE a.user_id =" . $curruser['id'] . " AND a.rame IN(0,9) AND b.open BETWEEN '{$minday}' AND '{$lastday}' GROUP BY b.open";
    }
    array_push($sqlArr, $sql);
}
$sql = implode(" UNION ALL ", $sqlArr);
$sql .= " ORDER BY open DESC ";
$rs = getdb($sql);
if ($rs->eof)
{
    $list = "<tr class=\"row1\"><td colspan=\"5\" align=\"center\">查无选择之日期区间下注资料</td></tr>";
}
else
{
    $list = "";
}
$dataArr = array();
while (!$rs->eof)
{
    $open = $rs->f['open'];
    if (isset($dataArr[$open]))
    {
        $dataArr[$open]['amount'] += $rs->f['amount'];
        $dataArr[$open]['a1'] += $rs->f['a1'];
        $dataArr[$open]['b1'] += $rs->f['b1'];
    }
    else
    {
        $dataArr[$open] = array();
        $dataArr[$open]['open'] = $rs->f['open'];
        $dataArr[$open]['amount'] = $rs->f['amount'];
        $dataArr[$open]['a1'] = $rs->f['a1'];
        $dataArr[$open]['b1'] = $rs->f['b1'];
    }
    $rs->movenext();
}
foreach ($dataArr as $val)
{
    $open = $val['open'];
    if ($begin < getPaperMinDate(0 - 1) && $end < getPaperMinDate(0 - 1))
    {
        $list .= "<tr class=td2><td colspan=5 align=center>查询日期小於可看最小报表日</td></tr>";
    }
    else
    {
        $list .= "<tr class=\"td2\">";
        if (0 < $val['amount'])
        {
            $showUrl = "?op=member_3d&fp=view_all&showDate=" . $open;
            $list .= "<td nowrap><a href=\"" . $showUrl . "\"><font color=\"#cc0000\"><b>" . $open . "</b></font></a></td>";
        }
        else
        {
            $list .= "<td nowrap><font color=\"#cc0000\">" . $open . "</font></td>";
        }
        $list .= "<td align=\"right\">" . $val['amount'] . "</td>";
        $list .= "<td align=\"right\">" . round($val['a1'], 2) . "</td>";
        $list .= "<td align=\"right\">" . say_numbercolor(round($val['b1'], 2)) . "</td>";
        $list .= "</tr>";
        $sum_amount += $val['amount'];
        $sum_result += $val['b1'];
        $sum_back1 += $val['a1'];
    }
    $rs->movenext();
}
$jslink = "<script>\r\n$(function(){\r\n  $('select#showGameType').change(function(){\r\n    _showGameType = $(this).val();\r\n    if(_showGameType == -1){window.top.mainFrame.location.replace('?op=member_3d&fp=history_all');}\r\n";
if ($g_init_mode_6h)
{
    $jslink .= "\r\n      else if(_showGameType == 0){\r\n        window.top.leftFrame.location.replace('?op=member&fp=account');\r\n        window.top.mainFrame.location.replace('?op=member&fp=history');\r\n      }\r\n  ";
}
$jslink .= "\r\n    else if(_showGameType >= 20){\r\n      window.top.leftFrame.location.replace('?op=member_5h&fp=account&gametype='+_showGameType);\r\n      window.top.mainFrame.location.replace('?op=member_5h&fp=history&gametype='+_showGameType);\r\n    }\r\n    else{\r\n      window.top.leftFrame.location.replace('?op=member_3d&fp=account&gametype='+_showGameType);\r\n      window.top.mainFrame.location.replace('?op=member_3d&fp=history&gametype='+_showGameType);\r\n    }\r\n  });\r\n});\r\n</script>\n\r\n";
$slcList = "";
$slcList = "<select name=\"showGameType\" id=\"showGameType\">";
$slcList .= "<option class=\"paperall\" value=\"-1\" selected=\"selected\">全部</option>";
if (!$g_init_mode_6h)
{
    unset($g_gametype_3d[0]);
}
if (!$g_init_mode_3d)
{
    foreach ($g_gametype_3d as $key => $value)
    {
        if ($key == 0)
        {
            continue;
        }
        unset($g_gametype_3d[$key]);
    }
}
foreach ($g_gametype_3d as $key => $val)
{
    $en = $val['en'];
    if ($g_menu_enabled[$key])
    {
        $slcList .= "<option class=\"paper{$en}\" value=\"{$key}\">";
        /*if ($key == 20)
        {
            $slcList .= "４+１码◎";
        }
        else if (20 < $key)
        {
            $slcList .= "八码◎";
        }
        else if ($key && $g_5number_3d)
        {
            $slcList .= getShow35($key) ? "五码◎" : "三码◎";
        }*/
        $slcList .= $val['ch'] . "</option>";
    }
}
$slcList .= "</select>";
$tpl->load("main", "history_all.htm");
$tpl->set(array("list" => $list, "sum_amount" => $sum_amount, "sum_result" => say_numbercolor(round($sum_result, 2)), "sum_back1" => round($sum_back1, 2), "sum_all" => round($sum_result + $sum_back1, 2), "gametypeStr" => "全部 ", "slcList" => $slcList));
$ui->set("jslink", $jslink);
$ui->header();
$tpl->show("main");
$ui->footer();
echo "\r\n\r\n";
?>